<template>
  <div id="pro-list">
    <el-scrollbar ref="scrollbarRef" height="500px" always>
      <el-table :data="proList" style="width: 100%">
        <el-table-column type="index" width="70" label="序号" fixed />
        <el-table-column fixed prop="banners" label="图片" width="150">
          <template #default="scope">
            <img :src="scope.row.img1" style="width: 100px; height: 70px" />
          </template>
        </el-table-column>
        <el-table-column fixed prop="proname" label="名称" width="150" />

        <el-table-column prop="category" label="分类" width="120" />
        <el-table-column prop="brand" label="品牌" width="120" />
        <el-table-column prop="originprice" label="价格" width="120" />
        <el-table-column prop="sales" label="销量" width="120" />
        <el-table-column prop="stock" label="库存" width="120" />
        <el-table-column label="是否售卖" fixed="right" width="120">
          <template #default="scope">
            <el-switch v-model="scope.row.issale"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否推荐" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isrecommend"
              @click="changeIsrecommend(scope.row.proid, scope.row.isrecommend)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否秒杀" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isseckill"
              @click="changeIsseckill(scope.row.proid, scope.row.isseckill)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default>
            <el-button>修改</el-button>
            <el-button>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>
    <div class="demo-pagination-block">
      <div class="demonstration"></div>
      <el-pagination
        v-model:currentPage="pageIndex"
        v-model:page-size="pageCount"
        :page-sizes="[5, 10, 15, 20]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="总页数, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script  lang="ts" setup>
import { computed, reactive, ref, watchEffect, onMounted } from "vue";
import { getProListApi, editUpdataFlagApi } from "@/api/pro";
import { ElMessage } from "element-plus";
import type { protype } from "@/mytype/index";
/* 定义产品列表的数据 */

/* 分页器 */

const small = ref(false);
const background = ref(false);
const disabled = ref(false);
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
};

// 每页的条数
let pageCount = ref(5);
// 当前页码
let pageIndex = ref(1);

let total = ref(0);

let proList = reactive<Array<protype>>([]);

function getProList(count: number = 1, limitNum: number = 5) {
  getProListApi({ count, limitNum }).then((res: any) => {
    if (res.code === "200") {
      proList.splice(0, pageCount.value, ...(res.data as Array<protype>));
      console.log("proList", proList);
      proList.forEach((item) => {
        if (item.isseckill == 1) {
          item.isseckill = true;
        } else {
          item.isseckill = false;
        }
      });
      proList.forEach((item) => {
        if (item.isrecommend == 1) {
          item.isrecommend = true;
        } else {
          item.isrecommend = false;
        }
      });
      proList.forEach((item) => {
        if (item.issale == 1) {
          item.issale = true;
        } else {
          item.issale = false;
        }
      });
      total.value = res.total;
      // proList.splice(0, 10, ...(res.data as Array<protype>))
    }
  });
}

onMounted(() => {
  getProList();
});

watchEffect(() => {
  // 当页码发生变化时，重新请求数据
  console.log("watchEffect.pageIndex.value", pageIndex.value);
  getProList(pageIndex.value, pageCount.value);
});

/* 改变是否推荐 */
function changeIsrecommend(id: string, flag: boolean) {
  console.log("flag", flag);
  // console.log("val", val);

  if (flag === true) {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isrecommend",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        console.log(res);
        ElMessage({
          message: "该商品已添加到推荐！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "添加失败！",
          type: "error",
        });
      }
    });
  } else {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isrecommend",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品取消推荐成功！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  }
}

/* 改变是否秒杀 */
function changeIsseckill(id: string, flag: boolean) {
  console.log("flag", flag);
  if (flag == true) {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isseckill",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品添加到秒杀！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  } else {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isseckill",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品取消秒杀成功！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  }
}
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>